package ms.platform.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * MD5加密解密
 * 
 * @author zhanghuibo
 *
 */
public class MD5Util {
	private static final Logger logger = LoggerFactory.getLogger(MD5Util.class);

	/***
	 * MD5加码 生成32位md5码
	 */
	public static String string2MD5(String inStr) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = (byte) charArray[i];
		}
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();

	}

	/**
	 * 加密解密算法
	 */
	public static String convertMD5(String inStr) {

		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String s = new String(a);
		return s;

	}

	/***
	 * MD5加密文件 生成32位md5码 首位如果是0则为31位
	 */
	public static String getMd5ByFile(File file) {
		String value = null;
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			MappedByteBuffer byteBuffer = in.getChannel().map(
					FileChannel.MapMode.READ_ONLY, 0, file.length());
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(byteBuffer);
			BigInteger bi = new BigInteger(1, md5.digest());
			value = bi.toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		int length = value.length();
		if (length != 32) {
			int a = 32 - length;
			for (int i = 0; i < a; i++) {
				value = "0" + value;
			}
		}
		return value;
	}

	/***
	 * 读取MD5加密文件
	 */
	public static String getMD5String(String jbhFileMD5Path) {
		String md5FileJBH = null;
		try {

			String encoding = "UTF-8";
			File file = new File(jbhFileMD5Path);
			// 读取文件
			if (file.isFile() && file.exists()) {// 判断文件是否存在

				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码问题
				BufferedReader bufferedReader = new BufferedReader(read);
				md5FileJBH = bufferedReader.readLine();
				read.close();
			} else {
				logger.debug("没有找到指定文件！！！");
			}
		} catch (Exception e) {
			logger.debug("读取文件内容出错!!!");
			e.printStackTrace();
		}
		return md5FileJBH;
	}
}
